package cn.appsys.controller.developer;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import cn.appsys.pojo.DevUser;
import cn.appsys.service.developer.DevUserService;
import cn.appsys.tools.Constants;

@Controller
@RequestMapping(value = "/dev")
public class developerLoginController {
	// 服务类接口
	@Resource
	DevUserService devUserService;

	// 登陆方法
	@RequestMapping(value = "/login")
	public String login(HttpServletRequest request) {
		// 跳转至后台登陆页面
		request.getSession().removeAttribute("error");
		return "devlogin";
	}

	// 登陆验证方法
	@RequestMapping(value = "/dologin")
	public String dologin(HttpServletRequest request, HttpSession session, @RequestParam("devCode") String devCode,
			@RequestParam("devPassword") String devPassword) {
		/* 调用登陆方法根据返回值确定是否登陆成功 */
		DevUser user = null;
		try {
			user = devUserService.login(devCode, devPassword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (null != user) {// 登录成功
			// 放入session
			session.setAttribute(Constants.DEV_USER_SESSION, user);
			// 页面跳转（main.jsp）
			return "/developer/main";
		} else {
			// 页面跳转（login.jsp）带出提示信息--转发
			request.setAttribute("error", "用户名或密码不正确");
			return "devlogin";
		}

	}

	/* 注销方法 */
	@RequestMapping(value = "/logout")
	public String logout(HttpServletRequest request) {
		/* 移除会话中的用户并重定向至首页 */
		request.getSession().removeAttribute("devUserSession");
		return "redirect:../index.jsp";
	}

}
